 #include "keyboard.h"

KEYBOARD_T keyboard_t;
/*****************************************************************************
 �� �� ��: keyboard_init
 ��������  : ����ɨ���ʼ������
 �������  : ��      
 �������  : ��
 �� �� ֵ: ��
 �޸���ʷ     :
  1.��    ��   : 2019��03��03��
    ��    ��   : ΢Ц����
    �޸�����   : ����
*****************************************************************************/
 void keyboard_init(void)
 {
    SET_Bit(KEY_ODR, KEY_LEFT);
    SET_Bit(KEY_ODR, KEY_RIGHT);
    SET_Bit(KEY_ODR, KEY_CHOICE);

    keyboard_t.scan_step    = 0;
    keyboard_t.choice_valve = 0;
    keyboard_t.left_valve   = 0;
    keyboard_t.right_value  = 0;
        
    return;
 }

/*****************************************************************************
 �� �� ��: keyboard_init
 ��������  : ����ɨ�账������
 �������  : ��      
 �������  : ��
 �� �� ֵ: ��
 �޸���ʷ     :
  1.��    ��   : 2019��03��03��
    ��    ��   : ΢Ц����
    �޸�����   : ����
*****************************************************************************/
 void keyboard_scan_process(void)
 {
    switch (keyboard_t.scan_step)
    {
        case 0:
        {
            if(GET_Bit(KEY_ODR, KEY_LEFT)  &&\
               GET_Bit(KEY_ODR, KEY_RIGHT) &&\
               GET_Bit(KEY_ODR, KEY_CHOICE))
            {
                return;
            }

            keyboard_t.delay_ms  = 50;
            keyboard_t.scan_step = 1;
        }
        break;
        case 1:
        {    
            if(0 < keyboard_t.delay_ms)
            {
                return;
            }
            if((GET_Bit(KEY_ODR, KEY_LEFT)  &&\
                GET_Bit(KEY_ODR, KEY_RIGHT) &&\
                GET_Bit(KEY_ODR, KEY_CHOICE)))
            {
                keyboard_t.scan_step = 0;
                return;
            }

            if(!GET_Bit(KEY_ODR, KEY_LEFT))
            {
                keyboard_t.left_valve ++;
                //uart_tx_data("key left!", 9);
            }
            else if(!GET_Bit(KEY_ODR, KEY_RIGHT))
            {
                keyboard_t.right_value ++;
                //uart_tx_data("key right!", 10);
            }
            else if(!GET_Bit(KEY_ODR, KEY_CHOICE))
            {
                //uart_tx_data("key choice!", 11);
                    keyboard_t.choice_valve ++;
            }
            keyboard_t.scan_step = 2;
        }
        break;
        case 2:
        {
            if(GET_Bit(KEY_ODR, KEY_LEFT)  &&\
               GET_Bit(KEY_ODR, KEY_RIGHT) &&\
               GET_Bit(KEY_ODR, KEY_CHOICE))
            {          
                keyboard_t.scan_step = 0;
            }
        }
        break;
        default:
            break;
    }
    return;
}